package com.asiainfo.cms.portal.controller;


import com.alibaba.fastjson.JSONObject;
import com.asiainfo.cms.common.config.CmsConfig;
import com.asiainfo.cms.common.result.CodeMsg;
import com.asiainfo.cms.common.result.Result;
import com.asiainfo.cms.common.util.file.FileUploadUtils;
import com.asiainfo.cms.common.util.file.FileUtils;
import com.asiainfo.cms.portal.entity.Banner;
import com.asiainfo.cms.portal.entity.CloudBusinessAlliance;
import com.asiainfo.cms.portal.entity.CoreProd;
import com.asiainfo.cms.portal.service.BannerService;
import com.asiainfo.cms.portal.service.CloudBusinessAllianceService;
import com.asiainfo.cms.portal.service.CoreProdService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 文章 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-04-12
 */
@RestController
@RequestMapping("/home")
public class HomePageController {

    @Resource
    private BannerService bannerService;
    @Resource
    private CoreProdService coreProdService;
    @Resource
    private CloudBusinessAllianceService cloudBusinessAllianceService;

    @RequestMapping(value = "/insertBanner", method = RequestMethod.POST)
    public Result insert(@RequestParam String title, @RequestParam String description,
                         @RequestParam Integer status, @RequestParam String link,
                         @RequestParam String module, MultipartFile file) throws Exception{

        Banner banner = new Banner();
        banner.setTitle(title);
        banner.setDescription(description);
        banner.setStatus(status);
        banner.setLink(link);
        banner.setModule(module);
        if (file != null){
            //FileUtils.deleteFile(banner.getImagePath().replace("http://118.195.141.71","/home/asiainfo"));
            String image_path = FileUploadUtils.upload(CmsConfig.getProfile() + "/bannerImage",file).replace("Constants.RESOURCE_PREFIX","http://118.195.141.71");
            banner.setImagePath(image_path);
        }
        bannerService.save(banner);
        return Result.success(banner);
    }

    //添加文章 修改
    @RequestMapping(value = "/getBanner", method = RequestMethod.GET)
    public Result getBanner() {

        List<Banner> banners = bannerService.getBannerByModule("index");
        return Result.success(banners);
    }

    @RequestMapping(value = "/deleteBanner", method = RequestMethod.POST)
    public Result delete( @RequestBody JSONObject requestData) {
        Integer id = requestData.getInteger("id");
        Banner banner = bannerService.getById(id);
        if (banner != null) {
            banner.setStatus(0);
            bannerService.removeById(banner);
            return Result.success(banner);
        }
        return Result.error(CodeMsg.ARTICLE_NOT_EXIST);
    }

    @RequestMapping(value = "/updateBanner", method = RequestMethod.POST)
    public Result update(@RequestParam String id, @RequestParam String title,
                         @RequestParam String description, @RequestParam Integer status,
                         @RequestParam String link, @RequestParam String module, MultipartFile file) throws Exception{
        Banner banner = bannerService.getById(id);
        if (banner != null) {
            banner.setTitle(title);
            banner.setDescription(description);
            banner.setStatus(status);
            banner.setLink(link);
            banner.setModule(module);
            if (file != null){
                FileUtils.deleteFile(banner.getImagePath().replace("http://118.195.141.71","/home/asiainfo/cms/file"));
                String image_path = FileUploadUtils.upload(CmsConfig.getProfile() + "/bannerImage",file).replace("Constants.RESOURCE_PREFIX","http://118.195.141.71");
                banner.setImagePath(image_path);
            }
            bannerService.updateById(banner);
            return Result.success(banner);
        }
        return Result.error(CodeMsg.ARTICLE_NOT_EXIST);
    }

    @RequestMapping(value = "/insertCoreProd", method = RequestMethod.POST)
    public Result insertCoreProd(@RequestParam String title, @RequestParam String description,
                         @RequestParam Integer status, @RequestParam String link, MultipartFile file) throws Exception{

        CoreProd coreProd = new CoreProd();
        coreProd.setTitle(title);
        coreProd.setDescription(description);
        coreProd.setStatus(status);
        coreProd.setLink(link);
        if (file != null){
            //FileUtils.deleteFile(banner.getImagePath().replace("http://118.195.141.71","/home/asiainfo"));
            String image_path = FileUploadUtils.upload(CmsConfig.getProfile() + "/picture",file).replace("Constants.RESOURCE_PREFIX","http://118.195.141.71");
            coreProd.setImagePath(image_path);
        }
        coreProdService.save(coreProd);
        return Result.success(coreProd);
    }

    //添加文章 修改
    @RequestMapping(value = "/getCoreProd", method = RequestMethod.GET)
    public Result getCoreProd() {

        List<CoreProd> coreProdList = coreProdService.list();
        return Result.success(coreProdList);
    }

    @RequestMapping(value = "/deleteCoreProd", method = RequestMethod.POST)
    public Result deleteCoreProd( @RequestBody JSONObject requestData) {
        Integer id = requestData.getInteger("id");
        CoreProd coreProd = coreProdService.getById(id);
        if (coreProd != null) {
            bannerService.removeById(coreProd);
            return Result.success(coreProd);
        }
        return Result.error(CodeMsg.ARTICLE_NOT_EXIST);
    }

    @RequestMapping(value = "/updateCoreProd", method = RequestMethod.POST)
    public Result updateCoreProd(@RequestParam String id, @RequestParam String title,
                         @RequestParam String description, @RequestParam Integer status,
                         @RequestParam String link, MultipartFile file) throws Exception{
        CoreProd coreProd = coreProdService.getById(id);
        if (coreProd != null) {
            coreProd.setTitle(title);
            coreProd.setDescription(description);
            coreProd.setStatus(status);
            coreProd.setLink(link);
            if (file != null){
                FileUtils.deleteFile(coreProd.getImagePath().replace("http://118.195.141.71","/home/asiainfo/cms/file"));
                String image_path = FileUploadUtils.upload(CmsConfig.getProfile() + "/picture",file).replace("Constants.RESOURCE_PREFIX","http://118.195.141.71");
                coreProd.setImagePath(image_path);
            }
            coreProdService.updateById(coreProd);
            return Result.success(coreProd);
        }
        return Result.error(CodeMsg.ARTICLE_NOT_EXIST);
    }

    @RequestMapping(value = "/insertCBA", method = RequestMethod.POST)
    public Result insertCBA(@RequestParam String companyName, @RequestParam String description,
                         @RequestParam Integer status, @RequestParam String link,
                         MultipartFile file) throws Exception{

        CloudBusinessAlliance businessAlliance = new CloudBusinessAlliance();
        businessAlliance.setCompanyName(companyName);
        businessAlliance.setDescription(description);
        businessAlliance.setStatus(status);
        businessAlliance.setLink(link);
        if (file != null){
            //FileUtils.deleteFile(banner.getImagePath().replace("http://118.195.141.71","/home/asiainfo"));
            String image_path = FileUploadUtils.upload(CmsConfig.getProfile() + "/picture",file).replace("Constants.RESOURCE_PREFIX","http://118.195.141.71");
            businessAlliance.setIcon(image_path);
        }
        cloudBusinessAllianceService.save(businessAlliance);
        return Result.success(businessAlliance);
    }

    //添加文章 修改
    @RequestMapping(value = "/getCBA", method = RequestMethod.GET)
    public Result getCBA() {

        List<CloudBusinessAlliance> alliances = cloudBusinessAllianceService.list();
        return Result.success(alliances);
    }

    @RequestMapping(value = "/deleteCBA", method = RequestMethod.POST)
    public Result deleteCBA( @RequestBody JSONObject requestData) {
        Integer id = requestData.getInteger("id");
        CloudBusinessAlliance alliance = cloudBusinessAllianceService.getById(id);
        if (alliance != null) {
            bannerService.removeById(alliance);
            return Result.success(alliance);
        }
        return Result.error(CodeMsg.ARTICLE_NOT_EXIST);
    }

    @RequestMapping(value = "/updateCBA", method = RequestMethod.POST)
    public Result updateCBA(@RequestParam String id, @RequestParam String companyName,
                         @RequestParam String description, @RequestParam Integer status,
                         @RequestParam String link, MultipartFile file) throws Exception{
        CloudBusinessAlliance alliance = cloudBusinessAllianceService.getById(id);
        if (alliance != null) {
            alliance.setCompanyName(companyName);
            alliance.setDescription(description);
            alliance.setStatus(status);
            alliance.setLink(link);
            if (file != null){
                FileUtils.deleteFile(alliance.getIcon().replace("http://118.195.141.71","/home/asiainfo/cms/file"));
                String image_path = FileUploadUtils.upload(CmsConfig.getProfile() + "/picture",file).replace("Constants.RESOURCE_PREFIX","http://118.195.141.71");
                alliance.setIcon(image_path);
            }
            cloudBusinessAllianceService.updateById(alliance);
            return Result.success(alliance);
        }
        return Result.error(CodeMsg.ARTICLE_NOT_EXIST);
    }


}
